home *** CD-ROM | disk | FTP | other *** search
/ Our Solar System / Our Solar System.iso / miscprog / targ / timer.c < prev   
Text File  |  1987-03-23  |  2KB  |  88 lines

  1. /* timer.c -- Contains wait(), which is a timed wait loop.
  2.               Caller specifies the time he wants to wait, in
  3.               tenths of a second.  IBM PC ROM-BIOS dependent!
  4.  
  5.    History: rhs 15-mar-87 ... Original
  6.             rhs 18-mar-87 ... Added wait_keytime(), which is based on wait().
  7.                               (For use with Targ-Hurt ESP Trainer.)
  8.  
  9. */
  10.  
  11.  
  12.  
  13. #include <math.h>
  14. #include <dos.h>
  15. #include <conio.h>
  16. #include "mancon.h"
  17. #include "declares.h"
  18.  
  19. long readclock();
  20. void wait(int);
  21.  
  22. /* wait -- loops for "tenths" tenths of a second, then returns.
  23. */           
  24. void wait(tenths)
  25. int tenths;
  26. {  long clocks, end, recent, remaining;
  27.    int newday=FALSE;
  28.    
  29.    clocks=(long)((float)tenths*1.821);
  30.    end=readclock(&newday)+clocks;
  31.    while( (recent=readclock(&newday)) < end )
  32.       if(newday) end=remaining;         /* newday=TRUE is returned only */
  33.       else remaining=end-recent;        /* once every 24 hours, at midnight */
  34. }
  35.    
  36.    
  37. /* wait_keytime -- loops for "tenths" tenths of a second, or until a key is
  38.                    pressed.
  39. */
  40. void wait_keytime(tenths)
  41. int tenths;
  42. {  long clocks, end, recent, remaining;
  43.    int newday=FALSE;
  44.    
  45.    clocks=(long)((float)tenths*1.821);
  46.    end=readclock(&newday)+clocks;
  47.    while( (recent=readclock(&newday)) < end && kbhit()==0 )
  48.       if(newday) end=remaining;         /* newday=TRUE is returned only */
  49.       else remaining=end-recent;        /* once every 24 hours, at midnight */
  50. }
  51.  
  52.  
  53.  
  54. static union REGS regs;
  55. /* readclock -- Use interrupt 26 to read the current system time.
  56.                 The time is returned in clocks (18.21 clocks per second.)
  57. */
  58. long readclock(newday)
  59. int *newday;
  60. {  *newday=FALSE;
  61.  
  62.    regs.h.ah=0;
  63.    int86(26,®s, ®s);
  64.    if(regs.h.al != 0) *newday=TRUE;
  65.    return( ((long)regs.x.cx<<16)+(long)regs.x.dx);
  66. }
  67.    
  68.  
  69. /* Test main */
  70. /*
  71. main(argc, argv)
  72. int argc;
  73. char *argv[];
  74. {  int pause=atoi(argv[1]);
  75.    char time[15];
  76.  
  77.    printf("beginning\n");
  78.    systime(time,11);
  79.    printf("%s\n", time);
  80.  
  81.    wait(pause);
  82.  
  83.    systime(time,11);
  84.    printf("%s\n", time);
  85.    printf("done");
  86. }
  87. */
  88.